package com.qf.controller;


import com.qf.entity.User;
import com.qf.enums.ExceptionInfoEnum;
import com.qf.exception.SsmException;
import com.qf.service.UserService;
import com.qf.util.SendMSMUtil;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import static com.qf.constant.SsmConstant.*;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SendMSMUtil sendMSMUtil;

    //转发到登录页面
    @RequestMapping("/register-ui")
    public String registerUI(){
        return "/user/register";
    }

    //异步校验用户名
    @RequestMapping("/check-username")
    @ResponseBody
    //1.不走视图解析器，直接响应数据给调用方
    //2.1如果返回的是Pojo类或Map,自动添加响应头application/json,charset=utf-8,并且自动转换成json字符串
    //2.2如果返回的是String/Integer的数据，响应头html/text;
    public ResultVO checkUsername(@RequestBody User user){
        userService.checkUsername(user.getUsername());
        return new ResultVO(0,"成功",null);
    }


    //发送手机验证码
    @PostMapping("/send-sms")
    @ResponseBody
    public ResultVO sendSMS(String phone, HttpSession session){
        if(phone==null||phone.length()!=11){
            log.info("【发送短信验证码】手机号码格式不正确={}",phone);
            throw new SsmException(ExceptionInfoEnum.PARAM_ERROR.getCode(),"手机号码不正确！！！");
        }
        //调用工具类发送验证码，拿到返回的code和msg,将正确的验证码放到session域中
        ResultVO vo = sendMSMUtil.sendSMS(phone,session);
        //响应数据
        return vo;
    }

    //执行注册
    @PostMapping("/register")
    @ResponseBody
    public ResultVO register(@Valid User user, BindingResult bindingResult,String registerCode ,HttpSession session){
        //1.校验验证码不能为null，并且与session域中的值是否一致
        String trueCode = (String) session.getAttribute(USER_CODE);
        if(registerCode==null||!registerCode.equals(trueCode)){
            //验证码为空，按验证码不正确
            log.info("【执行注册】验证码不正确，registerCode={},trueCode={}",registerCode,trueCode);
            throw new SsmException(ExceptionInfoEnum.CAPTCHA_ERROR);
        }
        //2，通过bindingResult验证用户名，密码，手机号，是否合法
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【执行注册】参数不合法，msg={},user={}",msg,user);
            throw new SsmException(ExceptionInfoEnum.PARAM_ERROR.getCode(),"参数不合法,msg="+msg);
        }
        //3.调用service层来，执行添加操作
        userService.register(user);
        //4.响应数据
        return new ResultVO(0,"成功",null);
    }

    //跳转到登录页面
    @GetMapping("/login-ui")
    public String loginUI(){
        return "/user/login";
    }

    //执行登录
    @PostMapping("/login")
    @ResponseBody
    public ResultVO login(String username,String password,HttpSession session){
//        2. 接收参数,并手动校验参数.
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(password)){
            log.info("【用户登录】用户名或密码不能为空，username={},password={}",username,password);
            throw new SsmException(ExceptionInfoEnum.PARAM_ERROR.getCode(),"用户名和密码均为必填项，岂能为空");
        }
//        3. 调用service执行登录. -> 接收到用户信息.
        User user = userService.login(username, password);
//        4. 将用户信息设置到session域中.
        session.setAttribute(USER_INFO,user);
//        5. 响应数据.
        return new ResultVO(0,"成功",null);
    }

}
